package net.zhengxinyun.performance.controller.indicators;

import io.swagger.models.auth.In;
import net.zhengxinyun.performance.Util.Util;
import net.zhengxinyun.performance.core.Result;
import net.zhengxinyun.performance.core.ResultUtils;
import net.zhengxinyun.performance.entity.TokenEntity;
import net.zhengxinyun.performance.entity.YearSummaryScoreEntity;
import net.zhengxinyun.performance.filter.DepartmentFilter;
import net.zhengxinyun.performance.filter.YearSummaryScoreFilter;
import net.zhengxinyun.performance.service.department.DepartmentService;
import net.zhengxinyun.performance.service.login.LoginService;
import net.zhengxinyun.performance.service.login.TokenService;
import net.zhengxinyun.performance.service.login.UserService;
import net.zhengxinyun.performance.service.yearSummaryScore.YearSummaryScoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/scoresummaryyear")
public class ScoreSummaryController {

    private YearSummaryScoreService yearSummaryScoreService;

    private TokenService tokenService;

    private UserService userService;

    private LoginService loginService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    public ScoreSummaryController(YearSummaryScoreService service, TokenService tokenService, UserService userService, LoginService loginService) {
        this.yearSummaryScoreService = service;
        this.tokenService = tokenService;
        this.userService = userService;
        this.loginService = loginService;
    }

    /**
     * 删除得分汇总
     *
     * @param param
     * @return
     */
    @PostMapping("/delete")
    public Result<String> delete(@RequestBody YearSummaryScoreParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            String str = yearSummaryScoreService.delete(param.yearSummaryScoreEntity);
            if (!"删除成功".equals(str)) {
                return ResultUtils.getResults(Result.Status.ERROR, str);
            } else {
                return ResultUtils.getSuccessResults();
            }
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    /**
     * 修改得分汇总
     *
     * @param param
     * @return
     */
    @PostMapping("/update")
    public Result<String> update(@RequestBody YearSummaryScoreParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            //根据传来的上级指标名设置上级指标id
            String res = yearSummaryScoreService.update(param.yearSummaryScoreEntity);
            if ("修改成功".equals(res)) {
                return ResultUtils.getSuccessResults();
            } else {
                return ResultUtils.getResults(Result.Status.ERROR, "更新失败，请联系管理员");
            }
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    @PostMapping("/insert")
    public Result<String> insert(@RequestBody YearSummaryScoreParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            YearSummaryScoreFilter filter = new YearSummaryScoreFilter();
            if (param.yearSummaryScoreEntity != null) {
                YearSummaryScoreEntity yearSummaryScoreEntity = param.yearSummaryScoreEntity;
                //根据传来的上级指标名设置上级指标id
                String res = yearSummaryScoreService.insert(yearSummaryScoreEntity);
                if ("添加成功".equals(res)) {
                    return ResultUtils.getSuccessResults();
                } else {
                    return ResultUtils.getResults(Result.Status.ERROR, "添加失败，请联系管理员");
                }
            } else if (param.list != null) {
                for (YearSummaryScoreEntity yearSummaryScoreEntity : param.list) {
                    filter.setYear(yearSummaryScoreEntity.getYear());
                    if (yearSummaryScoreService.query(filter).size() != 0) {
                        continue;
                    } else {
                        String res = yearSummaryScoreService.insert(yearSummaryScoreEntity);
                        if (!"添加成功".equals(res)) {
                            return ResultUtils.getResults(Result.Status.ERROR, "添加失败，请联系管理员");
                        }
                    }
                }
                return ResultUtils.getSuccessResults();
            }
            return ResultUtils.getResults(Result.Status.ERROR);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }

    /**
     * 查询
     *
     * @param param
     * @return
     */
    @PostMapping("/query")
    public Result<HashMap<String, Object>> query(@RequestBody YearSummaryScoreParam param) {
        if (tokenService.compareToken(param.tokenEntity.getValue())) {
            DepartmentFilter departmentFilter = new DepartmentFilter();
            List<DepartmentFilter> deptList = departmentService.query(departmentFilter);
            Map<String, DepartmentFilter> deptMap = new HashMap<>();
            for (DepartmentFilter dept : deptList) {
                if (!deptMap.containsKey(dept.getDepartcode())) {
                    deptMap.put(dept.getDepartcode(), dept);
                }
            }

            if (param.yearSummaryScoreFilter.getPageSize() == 0) {
                param.yearSummaryScoreFilter.setPageSize(10);
            }
            HashMap<String, Object> map = new HashMap<>();
            List<YearSummaryScoreFilter> list = yearSummaryScoreService.query(param.yearSummaryScoreFilter);
            for (YearSummaryScoreFilter y : list) {
                y.setDepartment(deptMap.get(y.getDeptCode()).getDepartment());
            }
            Integer i = yearSummaryScoreService.queryForCount(param.yearSummaryScoreFilter);
//            Double pageTotal = Util.intGetTotalPage(i, param.yearSummaryScoreFilter.getPageSize());
            //总条数
            map.put("pageTotal", i);
            map.put("list", list);//根据条件查询集合
            return ResultUtils.getSuccessResults(map);
        }
        return ResultUtils.getResults(Result.Status.OVERTIME, "token不对", null);
    }


}

class YearSummaryScoreParam {
    public TokenEntity tokenEntity;
    public YearSummaryScoreFilter yearSummaryScoreFilter;
    public YearSummaryScoreEntity yearSummaryScoreEntity;
    public List<YearSummaryScoreEntity> list;
}